from django.shortcuts import render

# Create your views here.
from rest_framework import mixins
from rest_framework import status
from rest_framework.filters import OrderingFilter
from rest_framework.generics import ListAPIView, RetrieveAPIView, CreateAPIView
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import GenericViewSet, ModelViewSet

from activities.models import Activity
from activities.views import ActivityListSerializer
from answers.models import QuesModel, QattrModel
from answers.serializers import AnswersatrrSerializer
from news.models import News, Category, NewsComment
from news.serializers import NewsSerializer, CategorySerializer, NewsDetailCommentSerializer,ListTuCaoSerializer, NewsCommentSerializer, NewsQuesSerializer
from tucao.models import Tucao
from users.models import User, Dynamics
from users.serializers import DynamicsSerializer


class NewsView(ListAPIView,CreateAPIView):
    """首页新闻列表视图"""

    queryset = News.objects.all().order_by('-clicks').select_related()
    serializer_class = NewsSerializer

    def post(self, request):

        try:
            user = request.user
        except Exception as e:
            return Response({"message": "参数错误"}, status=status.HTTP_400_BAD_REQUEST)


        #  获取参数
        content = request.data.get('content', None)
        title = request.data.get('title', None)
        category_id = request.data.get('category_id', None)

        print(content,title,category_id)
        # 校验参数
        if not all([content, title, category_id]):
            return Response({"message": "参数错误"}, status=status.HTTP_400_BAD_REQUEST)

        category = Category.objects.get(id=category_id)

        if not category:
            return Response({"message": "参数错误"}, status=status.HTTP_400_BAD_REQUEST)

        # 保存数据库
        news = News.objects.create(
            content=content,
            title=title,
            category=category,
            user=user,
        )

        # 序列化
        serializer = NewsDetailCommentSerializer(instance=news)

        # 返回
        return Response(serializer.data, status=status.HTTP_201_CREATED)


# url(r'^category/$', views.CategoryView.as_view()),
class CategoryView(APIView):
    """首页分类导航视图"""

    def get(self, request):
        categories = Category.objects.all()
        serializer = CategorySerializer(categories, many=True)
        return Response(serializer.data)


class NewsDetailCommentView(RetrieveAPIView):  #
    """新闻详情页视图"""

    queryset = News.objects.all().select_related("user").prefetch_related("news_comments")
    serializer_class = NewsDetailCommentSerializer


class CreateCommentView(ListAPIView):
    """发表评论"""

    def post(self, request):
        try:
            user = request.user
        except Exception as e:
            return Response({"message": "用户身份错误"}, status=status.HTTP_400_BAD_REQUEST)
        # 获取参数
        content = request.data.get('content', None)
        parent = request.data.get('parent', None)
        news_id = request.data.get('news_id', None)
        news = News.objects.get(id=news_id)
        # print(news)
        # 校验参数
        if not content:
            return Response({"message": "参数错误"}, status=status.HTTP_400_BAD_REQUEST)

        # newscomment = NewsComment.objects.get(id=news.id).all()

        newscomment = NewsComment.objects.create(
            content=content,
            user=user,
            parent=parent,
            news=news,
        )
        # data=NewsComment.objects.filter(news_id=news_id)
        serializer = NewsCommentSerializer(instance=newscomment)
        print(serializer.data)
        return Response(serializer.data,status=status.HTTP_201_CREATED)

class ListTuCaoView(ListAPIView):
    """详情中吐槽推荐视图"""
    queryset = Tucao.objects.all().order_by('-follow_count')[:5]
    serializer_class = ListTuCaoSerializer


class NewsQuesView(ListAPIView):
    """首页侧栏问答视图"""
    # queryset = QuesModel.objects.all().order_by('-qattrModel_set_click_count')[:5]
    # serializer_class = NewsQuesSerializer
    serializer_class = AnswersatrrSerializer
    def get_queryset(self):
        Qattrobjset = QattrModel.objects.all().order_by('-click_count')[:5]
        return Qattrobjset


class NewsActiviesView(ListAPIView):
    queryset = Activity.objects.all().order_by("-reads")[:2]
    serializer_class = ActivityListSerializer


class NewsPersonView(ListAPIView):
    queryset = Dynamics.objects.all()[0:5]
    serializer_class = DynamicsSerializer

    # serializer_class = NewsPersonSerializer
    # def get(self,request):
    #     # 当前用户为该新闻作者
    #     news_set = News.objects.get()
    #     user = User.objects.get(id="news_set.user.id")
    #     print(user)
    #
    #     dynamics_data = Dynamics.objects.filter(user=user)
    #     print(dynamics_data)
    #
    #     serializer = NewsPersonSerializer(dynamics_data)
    #     print(serializer.data)
    #     return Response(serializer.data)

# 获取当前页面的新闻　　　获取当前新闻的用户　　　获取当前用户的动态




